package com.yupi.springbootinit.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class SinglePublisher {


    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //设置客户端信息
        factory.setHost("139.159.245.136");
        factory.setPort(5672);
        factory.setUsername("xiaolaoban");
        factory.setPassword("161199");
        //创建连接
        try (Connection connection = factory.newConnection();
             //创建频道=》客户端
             Channel channel = connection.createChannel()) {
            //定义队列
            //durable队列持久化到硬盘，exclusive只有创建该队列的连接能用，autoDelete当队列没消息时自动删除
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
           //发送消息
            String message = "Hello World!";
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes(StandardCharsets.UTF_8));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
